#include <func.h>

int main(int argc, char* argv[])
{
	// 1. 先pipe
	int fields1[2];
	if (pipe(fields1) == -1) {
		error(1, errno, "pipe");
	}

	int fields2[2];
	if (pipe(fields2) == -1) {
		error(1, errno, "pipe");
	}

	char message[256];
	// 2. 再fork
	pid_t pid = fork();
	switch (pid) {
	case -1:
		error(1, errno, "fork");
	case 0:
		close(fields1[1]); // 关闭管道1的写端
		close(fields2[0]); // 关闭管道2的读端
		read(fields1[0], message, 256); // 阻塞点
		printf("Child: received '%s'\n", message);
		write(fields2[1], "done!", 6);

		_exit(0);
	default: 
		close(fields1[0]); // 关闭管道1的读端
		close(fields2[1]);// 关闭管道2的写端
		write(fields1[1], "homework done yet?", 19);
		read(fields2[0], message, 256);
		printf("Parent: received '%s'\n", message);

		exit(0);
	}
    return 0;
}

